<template>

    <div>
        <div>
            <el-button type="primary" @click="huanyuan" :disabled="selectlists.length == 0">还原</el-button>
            <el-button type="danger" @click="pldelete" :disabled="selectlists.length == 0">批量删除</el-button>
        </div>
        <RecycleTable :tableData="tableData" @recover="recover" @reset="deletes" @selectlist="selectlist">
        </RecycleTable>
        <Pagnition @changepage="changelist" :params="params" :total="total"></Pagnition>


    </div>
</template>
<script setup>
import { ElMessage, ElMessageBox } from 'element-plus'
import RecycleTable from "@/component/RecycleTable.vue"
import service from "@/utils/Request"
import { onMounted, ref } from "vue"
const selectlists = ref([])
const selectdata = ref([])
const params = ref({
    pageNo: 1,
    pageSize: 10
})
const tableData = ref([])
const total = ref(0)
const changelist = (e) => {
    console.log(e)
    params.value.pageNo = e.pageNo,
        params.value.pageSize = e.pageSize;
    gettablelist()
}
const recover = (e) => {
    let data = [];
    data.push(e.fileId)
    recoverservice(data)

}
const deletes = (e) => {
    let data = [];
    data.push(e.fileId)
    deleteservice(data)

}

const selectlist = (e) => {
    console.log(e)
    let data = [];
    selectlists.value = e
    selectlists.value.forEach(el => {
        data.push(el.fileId)
    });
    selectdata.value = data

}
const pldelete = () => {
    deleteservice(selectdata.value)
}
const huanyuan = () => {
    recoverservice(selectdata.value)
}
const deleteservice = (data) => {
    ElMessageBox.confirm(
        `是否确认删除?`,
        'Warning',
        {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
        }
    )
        .then(async () => {
            const res = await service.post("/recycle/delFile", {
                fileIds: data.join(",")
            })
            if (res.code == 1) {
                ElMessage({
                    type: "success",
                    message: "成功"
                })
                gettablelist()

            } else {
                ElMessage({
                    type: "warning",
                    message: "失败"
                })
            }
        })

}
const recoverservice = async (data) => {
    ElMessageBox.confirm(
        `是否确认还原?`,
        'Warning',
        {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
        }
    )
        .then(async () => {
            const res = await service.post("/recycle/recoverFile", {
                fileIds: data.join(",")
            })
            if (res.code == 1) {
                ElMessage({
                    type: "success",
                    message: "成功"
                })
                gettablelist()

            } else {
                ElMessage({
                    type: "warning",
                    message: "失败"
                })
            }
        })


}

const gettablelist = async () => {
    const res = await service.post("/recycle/loadRecycleList", params.value);
    console.log(res)
    tableData.value = res.data.records
    total.value = res.data.total
}
onMounted(() => {
    gettablelist()
})
</script>